package com.example.note_project.util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.note_project.entity.User;
import com.example.note_project.entity.note;

import java.util.ArrayList;


public class DBOpenHelper extends SQLiteOpenHelper {
    /**
     * 声明一个AndroidSDK自带的数据库变量db
     */
    private SQLiteDatabase db;


    public DBOpenHelper(Context context){
        super(context,"db_test",null,1);
        db = getReadableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE IF NOT EXISTS user(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "name TEXT," +
                "password TEXT)");
        db.execSQL("CREATE TABLE IF NOT EXISTS note(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "title TEXT," +
                "name TEXT," +
                "textHtml TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS user");
        db.execSQL("DROP TABLE IF EXISTS note");
        onCreate(db);
    }
    /**
     * 接下来写自定义的增删改查方法
     * 这些方法，写在这里归写在这里，以后不一定都用
     * addUser()
     * deleteUser()
     * updateUser()
     * getAllUser()
     */
    public void addUser(String name,String password){
        db.execSQL("INSERT INTO user (name,password) VALUES(?,?)",new Object[]{name,password});

    }
    public void deleteUser(String name,String password){
        db.execSQL("DELETE FROM user WHERE name = AND password ="+name+password);
    }
    public void updateUser(String password){
        db.execSQL("UPDATE user SET password = ?",new Object[]{password});
    }


    public ArrayList<User> getAllUser(){

        ArrayList<User> list = new ArrayList<User>();
        Cursor cursor = db.query("user",null,null,null,null,null,"name DESC");
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            list.add(new User(name,password));
        }
        return list;
    }
    public void addNote(String title,String dataStr,String name){
        db.execSQL("INSERT INTO note (title,texthtml,name) VALUES(?,?,?)",new Object[]{title,dataStr,name});
    }
    public void delteNote(String dataStr){
        db.execSQL("DELETE FROM note WHERE texthtml="+dataStr);
    }
    public void updateNote(String dataStr){
        db.execSQL("UPDATE note SET texthtml = ?",new Object[]{dataStr});
    }
    public ArrayList<note> getNote(String user){
        ArrayList<note> list = new ArrayList<note>();
        String selection = "name=?";
        String[] selectionArgs = { user };
        Cursor cursor = db.query("note",null,selection,selectionArgs,null,null,"title DESC");
        while(cursor.moveToNext()){
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String texthtml = cursor.getString(cursor.getColumnIndex("textHtml"));
            list.add(new note(title,texthtml));
        }

        return list;
    }
}
